interface IAddFeedbackViewModel extends ng.IScope {
    
      /**
     * 意见反馈模态框内信息(标题，意见内容)
     */
    modalTitle:string;
    modalContent:string;
    //提交按钮事件
    clickFeedback():void;//确定按钮事件
    /**
     * 意见反馈图片的上传组件
     */
    attachmentUploader: any;
    //意见反馈弹窗事件
    addFeedback():void

}

class AddFeedbackController {
   
    uploaderList:any[]
    ALLOWFILETYPE= [ 'gif', 'png', 'jpg', 'jpeg'];
    
    constructor(private $scope: IAddFeedbackViewModel,
                private ossUploaderService:ossUploaderService,
                private $rootScope: ImainRootScope,
                private $ngBootbox: BootboxService,
                private operationService: IOperationService) 
    {
        this.init();
        $('#opinionFeed').on('hide.bs.modal',()=>{
            this.showAddFeedback()
        })
    };

    init=()=>{
        const OssFile = this.ossUploaderService.getOssFile();

        this.$scope.attachmentUploader =new OssFile(this.$scope,this.$scope.attachmentUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});
        this.$scope.attachmentUploader.onSuccess = (fileItem: any) => {
             this.onSuccessItem(fileItem.fileName, fileItem.imgSrc, 1, fileItem.index); 
        }
        this.$scope.attachmentUploader.onDelete=(fileItem)=>{
            this.$scope.attachmentUploader.queue=this.$scope.attachmentUploader.queue.filter(it=>it.index!==fileItem.index);
            this.uploaderList=this.uploaderList.filter(it=>it.attachmentName!==fileItem.fileName||it.attachmentPath!==fileItem.imgSrc);
        }
        this.$scope.attachmentUploader.onAfterAddingFile = this.handleAddingFile;

        this.showAddFeedback();
    }
    /**
     * 选择上传文件后
     */
    onSuccessItem=(fileName: string, filePath: string, tag: number,index:number)=> {
        
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: "",
            index:index
        };
        this.uploaderList.push(attachment);

        this.uploaderList.sort((pre,after)=>{
            return pre.index-after.index;
        });
        console.log('list',this.uploaderList)
    }
    /**
     * 文件限制
     */
    handleAddingFile=(item)=>{
        if(item._file.size > 10*1024*1024){
            this.$ngBootbox.confirm('文件大小不能超过10M').then(result => {
                this.handleItemDelete(item);
            },reject => {
                this.handleItemDelete(item);
            });
            
        }else {
            var fileType = item.fileName.split('.').pop().toLowerCase();
            if(this.ALLOWFILETYPE.indexOf(fileType) === -1){
                this.$ngBootbox.confirm('不支持该文件类型').then(result => {
                    this.handleItemDelete(item);
                },reject=>{
                    this.handleItemDelete(item);
                })
            }
        }
    }
    /**
     * 删除附件
     */
    handleItemDelete = (item) =>{
        
        this.$scope.attachmentUploader.onDelete(item);
    }

    showAddFeedback=()=>{
            this.$scope.modalContent="";
            this.$scope.modalTitle="";
            //清空附件信息
            this.$scope.attachmentUploader.clearQueue();
            this.uploaderList=[];
            this.$scope.clickFeedback=this.addFeedback
    }

    addFeedback=()=>{
       

        if(this.$scope.modalTitle=="" || !this.$scope.modalTitle || this.$scope.modalTitle==null){
            this.$ngBootbox.alert('请输入标题');
            return;
        }else if(this.$scope.modalContent =="" || !this.$scope.modalContent || this.$scope.modalContent==null){
            this.$ngBootbox.alert('请输入反馈内容');
            return;
        }
        var uploaderList = []
        this.uploaderList.forEach((item, index) => {
            uploaderList.push([item.attachmentName, item.attachmentPath]);
        })

            this.operationService.addOpinion(
            this.$scope.modalTitle,
            this.$scope.modalContent,
            "1",//反馈平台选择1，PC
            uploaderList
            ).then((res) => {
                this.$ngBootbox.alert("反馈意见已提交，谢谢");
                $("#opinionFeed").modal("hide");
                this.$rootScope.$broadcast('addFeedback')
            })
    }

}

angular.module('tms').controller('AddFeedbackCtrl', ['$scope', 'ossUploaderService','$rootScope','$ngBootbox','operationService', AddFeedbackController])